import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:flutter_svg/flutter_svg.dart';
import 'package:squat_easy/se_service/se_log_service.dart';
import 'package:ylm_ui_kit/ylm_ui_kit.dart';

//主tab框架
class SeHomeScaffold extends StatelessWidget {
  SeHomeScaffold(
    this.controller,
    this.items,
    this.onPageChanged,
    this.checkedColor,
    this.normalColor, {
    this.physics,
    Key? key,
  }) : super(key: key);

  final CommonHomeScaffoldController controller;
  final List<CommonHomeScaffoldItem> items;
  final ValueChanged<int> onPageChanged;
  final Color checkedColor;
  final Color normalColor;
  final ScrollPhysics? physics;

  final double safeBottom = ScreenUtil().bottomBarHeight * 0.5;
  static final double itemsHeight = 60.w;

  @override
  Widget build(BuildContext context) {
    var content = PageView(
      physics: physics ?? const NeverScrollableScrollPhysics(),
      controller: controller._pageController,
      onPageChanged: (page) {
        controller._curPage = page;
        controller._refreshTab();
        onPageChanged(page);
        SeLogManager.stat(page: 'tab-${items[page].name}');
      },
      children: items.map((e) => e.page).toList(),
    );

    return Stack(
      alignment: Alignment.bottomCenter,
      children: [
        content.paddingBottom(itemsHeight + safeBottom),
        _buildBottomBar(),
      ],
    );
  }

  ///tabBar
  Widget _buildBottomBar() {
    return SizedBox(
      height: safeBottom + itemsHeight,
      child: ValueListenableBuilder(
        valueListenable: controller._tabController,
        builder: (context, value, Widget? child) {
          List<Widget> children = items.map((e) => _item(e)).toList();
          return Container(
            height: safeBottom + itemsHeight,
            decoration: CommonDecoration.getRadius(topLeft: 18, topRight: 18, bg: Colors.white),
            padding: EdgeInsets.only(bottom: safeBottom),
            child: Row(children: children),
          );
        },
      ),
    );
  }

  Widget _item(CommonHomeScaffoldItem item) {
    double w = (ScreenUtil().screenWidth / 4.0).floorToDouble();
    bool selected = controller.curPage == items.indexOf(item);
    String icon = selected ? item.selectedIcon : item.icon;
    Widget btn = ButtonWidget(
      onTap: () => controller._pageController.jumpToPage(items.indexOf(item)),
      alignment: Alignment.center,
      hasInkWell: false,
      width: w,
      child: SvgPicture.asset(icon, width: 32, height: 32),
    );
    return btn;
  }
}

class CommonHomeScaffoldController {
  CommonHomeScaffoldController(int initialPage)
      : _curPage = initialPage,
        _pageController = PageController(initialPage: initialPage);

  final PageController _pageController;
  final ValueNotifier<bool> _tabController = ValueNotifier(false);
  int _curPage;

  int get curPage => _curPage;

  void refreshBadge() => _refreshTab();

  void _refreshTab() {
    _tabController.value = !_tabController.value;
  }

  void changeIndex(int index) => _pageController.jumpToPage(index);
}

class CommonHomeScaffoldItem {
  CommonHomeScaffoldItem(this.page, this.name, this.icon, this.selectedIcon, this.hasPoint, [this.logic]);

  Widget page;
  String name;
  String icon;
  String selectedIcon;
  String Function() hasPoint;
  dynamic logic;
}
